package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author readpage
 * @create 2022-09-11 22:07
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Goods implements Comparable {
    private String name;
    private double price;

    //指明商品比较大小的方式:按照价格从低到高排序,再按照产品名称从高到低排序
    @Override
    public int compareTo(Object o) {
        if (o instanceof Goods) {
            Goods goods = (Goods)o;
            if (this.price > goods.price) {
                return 1;
            } else if (this.price < goods.price) {
                return -1;
            } else {
//                return 0;
                return -this.name.compareTo(goods.name);
            }
            //方式二:
//            return Double.compare(this.price, goods.price);
        }
        throw new RuntimeException("传入的数据类型不一致!");
    }
}
